<?php
	if (session_id() == '') {
		    session_start();
		}
	
	if(isset($_REQUEST["getUserID"])){
		echo json_encode($_SESSION['userid']);
	  }
	
	if(isset($_POST["isLoggedin"])){
		if(isset($_SESSION['userid'])){
			echo json_encode(true);
		}else{
			echo json_encode(false);
		}
	}
	if(isset($_POST["getUsername"])){
		echo json_encode($_SESSION["username"]);
	}
	


	//update Pictures table, use when added new pictures
	include ("config.php");
	global $yhendus;
	$yhendus = new mysqli($host, $user, $pw, $db);
	
	if(isset($_REQUEST['version'])){
		echo 'Current PHP version: ' . phpversion();
	}
	
	//Get Tabu Words of the picture
	
	if(isset($_POST["getTaboo"])){
		
		/*$sql="SELECT id FROM answer WHERE picture_id = ? ORDER BY id DESC LIMIT 40";
		$array=array();
		
		$kask=$yhendus->prepare($sql);
		$kask->bind_param("i", $_POST["getTaboo"]);
		$kask->bind_result($id);
		$kask->execute();
		while($kask->fetch()){
			array_push($array, $id);
		}
		*/
		$sql2="SELECT answer, COUNT(*) 
		AS count FROM answer WHERE
		 picture_id = ? 
		 and 
		 id > (SELECT id FROM answer WHERE 
			 picture_id = ? ORDER BY
			 id DESC LIMIT 1 OFFSET 20)
		  GROUP BY answer ORDER BY COUNT(*) DESC LIMIT 5
		";
		
		$kask=$yhendus->prepare($sql2);
		$kask->bind_param("ii", $_POST["getTaboo"], $_POST["getTaboo"]);
		$kask->bind_result($answer, $count);
		$kask->execute();
		print_r($kask->error);
		$array2=array();
		
		while($kask->fetch()){
			array_push($array2, array($answer, $count));
			
		}
		
		$result=array();
		$lenght = sizeof($array2);
		
		for($i=0; $i<$lenght; $i++){
			if($array2[$i][1]>2){
				array_push($result, $array2[$i][0]);
			}
		}
		
		echo json_encode($result);
		$yhendus->close();
		
		
	}
	
	if(isset($_POST['getScore'])){
		$kask=$yhendus->prepare("Select points FROM Balance Where User_id=?");
		$kask->bind_param("i", $_SESSION['userid']);
		$kask->bind_result($points);
		$kask->execute();
		$kask->fetch();
		$kask->close();
		
		echo json_encode($points);	
		$yhendus->close();
	}
	
	
	
	
	
	// Password for updating picture list in database is Secret
	// Usage http://[Host]/Picturegame/functions.php?UpdatePictures=secret
	if(isset($_REQUEST["UpdatePictures"])&&$_REQUEST["UpdatePictures"]=="secret"){
		$array = array();
		$array2 = array();
		$result = array();
		$array = glob('./images/*.{jpeg,gif,png,jpg}', GLOB_BRACE);
		$array2= glob('./images/*/*.{jpeg,gif,png,jpg}', GLOB_BRACE);
		$result= array_merge($array, $array2);

		$yhendus = new mysqli($host, $user, $pw, $db);
		$kask=$yhendus->prepare("TRUNCATE TABLE picture");
		$kask->execute();
		
		$kask=$yhendus->prepare("INSERT INTO picture (url) VALUES (?)");
		foreach ($result as $value){
			$kask->bind_param("s", $value);
			$kask->execute();
		}
		$yhendus->close();
		print_r ($result);
		
	}
	
	//Get pictures count in database
	
	if(isset($_POST["getPicturesLenght"])){
		$kask=$yhendus->prepare("SELECT COUNT(*) FROM picture");
		$kask->bind_result($count);
		$kask->execute();
		$kask->fetch();
		echo json_encode($count);
		$yhendus->close();	
	}
	
	
	
	//Get picture
	if(isset($_POST["getPicture"])){
		$kask=$yhendus->prepare("SELECT id, url FROM picture Where id=?");
		$kask->bind_param("i", $_POST["getPicture"]);
		$kask->bind_result($id, $url);
		$kask->execute();
		$kask->fetch();
		echo json_encode($url);		
		$yhendus->close();
	}
	
	//Get Tabu Words of the picture
	/*
	if(isset($_POST["getTabuwords"])){
		$array=array();
		$kask=$yhendus->prepare("SELECT word FROM tabu_word 
			JOIN picture_tabowords as a 
			ON tabu_word.id=a.tabu_word_id where a.picture_id=? and a.valid=1");
		$kask->bind_param("i", $_POST["getTabuwords"]);
		$kask->bind_result($word);
		$kask->execute();
		while($kask->fetch()){
			array_push($array, $word);
		}
		echo json_encode($array);
	}*/
	
	//Send User Answers on pictures
	if(isset($_POST["sendAnswers"])){
		$kask=$yhendus->prepare("INSERT INTO answer(User_id, picture_id, answer) VALUES (?,?,?)");
		$array=json_decode($_POST["sendAnswers"]);
		$lenght = sizeof($array);
		for($i=1; $i<$lenght; $i++){
			$kask->bind_param("iis", $_SESSION['userid'], $array[0], $array[$i]);
			$kask->execute();
		}
		echo json_encode($kask->error);
		$yhendus->close();
	}
	
	//$yhendus->close();
	
	//Get userID
	




?>